import $ from '../../public/static/jquery-1.12.1.min'

// 初始化插件
var version = "websdk3.220200615"
var g_iWndIndex = 0;
var g_bPTZAuto = false;
var szDeviceIdentify = "192.168.10.11_80";

export default {
    // 停止预览
    clickStopRealPlay: () =>
        new Promise(function(resolve, reject) {
            var szInfo = "";
            WebVideoCtrl.I_Stop({
                success: function() {
                    szInfo = "停止预览成功！";
                    console.log(szInfo);
                    resolve()
                },
                error: function(error) {
                    szInfo = "停止预览失败！";
                    console.log(szInfo);
                    reject(error)
                }
            });
        }),
    init: function(width, height) {
        // 检查插件是否已经安装过
        var iRet = window.WebVideoCtrl.I_CheckPluginInstall();
        if (-1 == iRet) {
            console.log("您还未安装过插件，双击开发包目录里的WebComponentsKit.exe安装！");
            return;
        }

        // 初始化插件参数及插入插件
        WebVideoCtrl.I_InitPlugin(width, height, {
            bWndFull: true, //是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
            iPackageType: 2,
            //szColorProperty:"plugin-background:0000ff; sub-background:0000ff; sub-border:00ffff; sub-border-select:0000ff",   //2:PS 11:MP4
            iWndowType: 1,
            // proxyAddress:{
            //    port:"80",  //摄像头端口
            //    ip:"10.18.99.194",//摄像头ip
            //    wsport:"7681"//摄像头ws端口
            //},
            bNoPlugin: true,
            cbEvent: function(iEventType, iParam1, iParam2) {
                if (2 == iEventType) { // 回放正常结束
                    console.log("窗口" + iParam1 + "回放结束！");
                } else if (-1 == iEventType) {
                    console.log("设备" + iParam1 + "网络错误！");
                } else if (3001 == iEventType) {
                    console.log("停止录像");
                }
            },
            cbRemoteConfig: function() {
                console.log("关闭远程配置库！");
            },
            cbInitPluginComplete: function() {
                console.log('??', WebVideoCtrl)
                WebVideoCtrl.I_InsertOBJECTPlugin("example_video_1");
                console.log('WebVideoCtrl.I_CheckPluginVersion() ->', WebVideoCtrl.I_CheckPluginVersion())

                // 检查插件是否最新
                if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
                    alert("检测到新的插件版本，双击开发包目录里的WebComponentsKit.exe升级！");
                    return;
                }
            }
        });

        // 初始化日期时间
        // var szCurTime = dateFormat(new Date(), "yyyy-MM-dd");


        this.clickLogin(this.clickStartRealPlay)
    },

    // 登录
    clickLogin: function(cb) {
        var szIP = '192.168.10.11',
            szPort = '80',
            szUsername = 'admin',
            szPassword = 'abc123456';

        if ("" == szIP || "" == szPort) {
            return;
        }

        console.log('登录```', szIP, 1, szPort, szUsername, szPassword)
        var iRet = WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {
            success: function(xmlDoc) {
                console.log('登陆成功', xmlDoc)
                    /* setTimeout(function () {
                         $("#ip").val(szDeviceIdentify);
                         getChannelInfo();
                         getDevicePort();
                     }, 10);*/
                cb && cb()
            },
            error: function(status, xmlDoc) {
                console.log('登陆失败', status, xmlDoc)
            }
        });
        if (-1 == iRet) {
            console.log('已登录过')
            cb && cb()
        }
    },

    // 开始预览
    clickStartRealPlay: function(iStreamType = 1) {
        var szDeviceIdentify = '192.168.10.11_80',
            iRtspPort = 554,
            iChannelID = 1,
            bZeroChannel = false,
            szInfo = "";

        // if (!szDeviceIdentify) {
        //     return;
        // }

        var startRealPlay = function() {
            console.log('开始预览2', szDeviceIdentify, iRtspPort, iStreamType, iChannelID, bZeroChannel)
            WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {
                iRtspPort,
                iStreamType,
                iChannelID,
                bZeroChannel,
                success: function() {
                    console.log('预览成功')
                },
                error: function(status, xmlDoc) {
                    if (403 === status) {
                        szInfo = "设备不支持Websocket取流！";
                    } else {
                        szInfo = "开始预览失败！";
                    }
                }
            });
        };

        startRealPlay();
    },

    // PTZ控制 9为自动，1,2,3,4,5,6,7,8为方向PTZ
    mouseDownPTZControl: function(iPTZIndex, iPTZSpeed = 4) {
        var bZeroChannel = false

        // if (bZeroChannel) {// 零通道不支持云台
        //     return;
        // }

        if (9 == iPTZIndex && g_bPTZAuto) {
            iPTZSpeed = 0; // 自动开启后，速度置为0可以关闭自动
        } else {
            g_bPTZAuto = false; // 点击其他方向，自动肯定会被关闭
        }

        WebVideoCtrl.I_PTZControl(iPTZIndex, false, {
            iPTZSpeed: iPTZSpeed,
            success: function(xmlDoc) {
                if (9 == iPTZIndex && g_bPTZAuto) {
                    console.log(" 停止云台成功！");
                } else {
                    console.log(" 开启云台成功！");
                }
                if (9 == iPTZIndex) {
                    g_bPTZAuto = !g_bPTZAuto;
                }
            },
            error: function(status, xmlDoc) {
                console.log(" 开启云台失败！", status, xmlDoc);
            }
        });
    },

    // 方向PTZ停止
    mouseUpPTZControl: function() {
        WebVideoCtrl.I_PTZControl(1, true, {
            success: function(xmlDoc) {
                console.log(" 停止云台成功！");
            },
            error: function(status, xmlDoc) {
                console.log(" 停止云台失败！", status, xmlDoc);
            }
        });
    },

    // 变倍+
    PTZZoomIn: function() {
        WebVideoCtrl.I_PTZControl(10, false, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 调焦+成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  调焦+失败！", status, xmlDoc);
            }
        });
    },
    // 变倍-
    PTZZoomout: function() {

        WebVideoCtrl.I_PTZControl(11, false, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 调焦-成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  调焦-失败！", status, xmlDoc);
            }
        });
    },
    // 变焦停止
    PTZZoomStop: function() {

        WebVideoCtrl.I_PTZControl(11, true, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 调焦停止成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  调焦停止失败！", status, xmlDoc);
            }
        });
    },
    // 变焦+
    PTZFocusIn: function() {
        WebVideoCtrl.I_PTZControl(12, false, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 聚焦+成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  聚焦+失败！", status, xmlDoc);
            }
        });
    },
    // 变焦-
    PTZFoucusOut: function() {
        WebVideoCtrl.I_PTZControl(13, false, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 聚焦-成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  聚焦-失败！", status, xmlDoc);
            }
        });
    },
    // 变焦停止
    PTZFoucusStop: function() {

        WebVideoCtrl.I_PTZControl(12, true, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 聚焦停止成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  聚焦停止失败！", status, xmlDoc);
            }
        });
    },

    // 光圈+
    PTZIrisIn: function() {

        WebVideoCtrl.I_PTZControl(14, false, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 光圈+成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  光圈+失败！", status, xmlDoc);
            }
        });
    },
    // 光圈-
    PTZIrisOut: function() {

        WebVideoCtrl.I_PTZControl(15, false, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 光圈-成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  光圈-失败！", status, xmlDoc);
            }
        });
    },
    // 光圈停止
    PTZIrisStop: function() {
        WebVideoCtrl.I_PTZControl(14, true, {
            iWndIndex: g_iWndIndex,
            success: function(xmlDoc) {
                console.log(" 光圈停止成功！");
            },
            error: function(status, xmlDoc) {
                console.log("  光圈停止失败！", status, xmlDoc);
            }
        });
    }

}